// https://leetcode.cn/problems/prime-arrangements/
// Created by ade on 2022/10/31.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <math.h>


using namespace std;

class Solution {
public:
    int M = 1e9 + 7;

    int numPrimeArrangements(int n) {
        if (n <= 2) return 1;
        vector<int> m(n + 1, 0);
        iota(m.begin(), m.end(), 0);
        for (int i = 2; i <= n / 2; i++) {
            int j = 2;
            while (j * i <= n) {
                m[j * i] = 0;// 代表不是质数
                j++;
            }
        }
        int zcount = 0;
        for (int i = 2; i <= n; i++) if (m[i] != 0) zcount++;
        return int(factorial(zcount) * factorial(n - zcount) % M);
    }

    long factorial(int num) {
        long res = 1;
        for (int i = 1; i <= num; i++) {
            res *= i;
            res %= M;
        }
        return res;
    }
};

int main() {
    Solution so;
    cout << so.numPrimeArrangements1(11) << endl;
    return 0;
}